package com.scalar.chiptrack.operations.process.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.operations.process.BomPartNumbersAndQuantity;
import com.scalar.chiptrack.operations.process.BomSetup;
import com.scalar.chiptrack.operations.process.FinishedGoods;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.process.dao.BomPartEntryDAO;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.process.forms.BomSetupForm;

/**
 * Created by KumaraSwamy.
 * Date: May 12, 2005
 * Time: 6:09:44 PM
 */

public final class BomSetupAction extends ChipTrackAction
{

    public ActionForward execute ( ActionMapping mapping , ActionForm form ,
                                   HttpServletRequest request , HttpServletResponse response ) throws Exception
    {
        String action = request.getParameter ( "action" );
        if ( action == null )
        {
            action = "";
        }

        BomSetupForm bomSetupForm               = ( BomSetupForm ) form;
        BomSetup bomSetup                       = null;
        HashMap<String, String> info            = new HashMap<String, String> ();
        HashMap<String, String> errors          = new HashMap<String, String> ();
        ArrayList<LabelValueBean> partNumberSelectList 	= new ArrayList<LabelValueBean>();
        String retVal	                        = "bomSetup";

        try
        {
            if( bomSetupForm.getProcessType().equalsIgnoreCase(Constants.LOCATION_TYPE_ASSEMBLY))
            {
                ArrayList<PartNumber> partNumberList  = ProductDao.getPartNumber( Constants.LOCATION_TYPE_ASSEMBLY );
                PartNumber partNumber	= null;
                for( int i = 0; i < partNumberList.size(); i++ )
                {
                    partNumber = (PartNumber)partNumberList.get(i);
                    partNumberSelectList.add( new org.apache.struts.util.LabelValueBean(partNumber.getPartNumber(), partNumber.getProcessStepId() + "" ) );
                }
            }
            else if (Constants.LOCATION_TYPE_FINISHED_GOODS.equalsIgnoreCase(bomSetupForm.getProcessType()))
            {
                ArrayList<FinishedGoods> partNumberList  = ProductDao.loadAllFinishedGoods();
                FinishedGoods partNumber	= null;
                for( int i = 0; i < partNumberList.size(); i++ )
                {
                    partNumber = (FinishedGoods)partNumberList.get(i);
                    partNumberSelectList.add( new org.apache.struts.util.LabelValueBean(partNumber.getManufacturingPartNo(), partNumber.getProcessStepId() + "" ) );
                }
            } else if (Constants.PRODUCT_TYPE_CHIPSET.equalsIgnoreCase(bomSetupForm.getProcessType())) {
                partNumberSelectList = NewProductDAO.getPartNumbersByProcessParameter(null, Constants.LOCATION_TYPE_FINISHED_GOODS, Constants.CHIPSET_PROCESS_PARAMETER_NAME, "true" );
            }
        }
        catch ( ScalarException e )
        {
            errors.put( "Error while Loading the AssemblyPartNumbersList ", null );
        }
        if ( ( action != null ) && ( action.equalsIgnoreCase( "BomSetup" ) ) )
        {
            bomSetup = new BomSetup();
            bomSetupForm.setBomSetup( bomSetup );
            request.setAttribute ( "blankBom" , "false" );
        }
        else if ( ( action != null ) && ( action.equalsIgnoreCase ( "OpenBomPartNumbers" ) ) )
        {
	        if( bomSetupForm.getBomSetup().getAssemblyProcessStepId() != -1 && bomSetupForm.getBomSetup().getAssemblyProcessStepId() != 0 )
            {
                try
                {
                    BomPartEntryDAO.loadBomPartNumbers( bomSetupForm );
                }
                catch ( ScalarException e )
                {
                    errors.put( "Error while Loading the BomPartNumbers ", null );
                }
                if( bomSetupForm.getBomSetup().getBomPartNumbersAndQuantitylist().size() == 0 )
                {
                    if( bomSetupForm.getProcessType().equalsIgnoreCase(Constants.LOCATION_TYPE_ASSEMBLY ))
                    {
                        bomSetupForm.getBomSetup().getBomPartNumbersAndQuantitylist().add( new BomPartNumbersAndQuantity(-1, "1") );
                    }
                    else if( bomSetupForm.getProcessType().equalsIgnoreCase(Constants.LOCATION_TYPE_FINISHED_GOODS))
                    {
                        bomSetupForm.getBomSetup().getBomPartNumbersAndQuantitylist().add( new BomPartNumbersAndQuantity() );
                    } else if( bomSetupForm.getProcessType().equalsIgnoreCase(Constants.PRODUCT_TYPE_CHIPSET)) {
                        bomSetupForm.getBomSetup().getBomPartNumbersAndQuantitylist().add( new BomPartNumbersAndQuantity() );
                    }
                }
                request.setAttribute ( "BomPartNumbers" , BomPartEntryDAO.loadAllBomPartNumbers ( bomSetupForm.getProcessType().equalsIgnoreCase(Constants.LOCATION_TYPE_ASSEMBLY) ? Constants.LOCATION_TYPE_DIE_BANK : bomSetupForm.getProcessType(), "-1") );
                request.setAttribute ( "blankBom" , "TRUE" );
            }
            else
            {
                bomSetup = new BomSetup();
                bomSetupForm.setBomSetup( bomSetup );
                request.setAttribute ( "blankBom" , "false" );
            }
        }
        else if ( action.equalsIgnoreCase ( "addmoreBomPartNumbers" ) )
        {
            request.setAttribute ( "BomPartNumbers" , BomPartEntryDAO.loadAllBomPartNumbers (bomSetupForm.getProcessType().equalsIgnoreCase(Constants.LOCATION_TYPE_ASSEMBLY) ? Constants.LOCATION_TYPE_DIE_BANK : bomSetupForm.getProcessType(), "-1") );
            if( bomSetupForm.getProcessType().equalsIgnoreCase(Constants.LOCATION_TYPE_ASSEMBLY))
            {
                bomSetupForm.getBomSetup().getBomPartNumbersAndQuantitylist().add( new BomPartNumbersAndQuantity(-1, "1") );
            }
            else if( bomSetupForm.getProcessType().equalsIgnoreCase(Constants.LOCATION_TYPE_FINISHED_GOODS))
            {
                bomSetupForm.getBomSetup().getBomPartNumbersAndQuantitylist().add( new BomPartNumbersAndQuantity() );
            } else if( bomSetupForm.getProcessType().equalsIgnoreCase(Constants.PRODUCT_TYPE_CHIPSET)) {
                bomSetupForm.getBomSetup().getBomPartNumbersAndQuantitylist().add( new BomPartNumbersAndQuantity() );
            }
            request.setAttribute ( "blankBom" , "TRUE" );
        }
        else if ( ( action.equalsIgnoreCase ( "SaveBoardMaking" ) ) )
        {
            try
			{
				BomPartEntryDAO.saveBomPartNumbers( bomSetupForm );
                info.put( "Data has been updated successfully", null );
			}
			catch ( ScalarException e )
			{
			    errors.put( "Bom Setup Details Could not saved ", null );
			}
            bomSetup = new BomSetup();
            bomSetupForm.setBomSetup( bomSetup );
            request.setAttribute ( "blankBom" , "false" );
        }
        else if ( ( action.equalsIgnoreCase ( "delete" ) ) )
        {
            try
			{
                int bomProcessStepId = Integer.parseInt(request.getParameter("bomProcessStepId"));
                BomPartEntryDAO.deleteBomPartNumbers( bomSetupForm.getBomSetup().getAssemblyProcessStepId(),  bomProcessStepId);
                info.put( "Data has been updated successfully", null );
			}
			catch ( ScalarException e )
			{
			    errors.put( "Bom Setup Details Could not saved ", null );
			}
            bomSetup = new BomSetup();
            bomSetupForm.setBomSetup( bomSetup );
            request.setAttribute ( "blankBom" , "false" );
        }
        if ( !errors.isEmpty () )
        {
            request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
        }
        if ( !info.isEmpty () )
        {
            request.setAttribute( SessionKeys.INFO_MESSAGES, info );
        }
        request.setAttribute("processTypes", getProcessTypes());
        request.setAttribute( "partNumberSelectList", partNumberSelectList );
        return ( mapping.findForward ( retVal ) );
    }

    private ArrayList getProcessTypes()
    {
        ArrayList processTypes = new ArrayList();
        processTypes.add(new LabelValueBean(Constants.LOCATION_TYPE_ASSEMBLY, Constants.LOCATION_TYPE_ASSEMBLY));
        processTypes.add(new LabelValueBean(Constants.LOCATION_TYPE_FINISHED_GOODS, Constants.LOCATION_TYPE_FINISHED_GOODS));
        processTypes.add(new LabelValueBean(Constants.PRODUCT_TYPE_CHIPSET, Constants.PRODUCT_TYPE_CHIPSET));
        return processTypes;
    }

}